package com.huage.courseplatform.application.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huage.courseplatform.application.mapper.BusinessMapper;
import com.huage.courseplatform.application.mapper.CourseMapper;
import com.huage.courseplatform.application.mapper.PeriodMapper;
import com.huage.courseplatform.application.model.dto.CourseDTO;
import com.huage.courseplatform.application.model.dto.PeriodDTO;
import com.huage.courseplatform.application.model.entity.*;
import com.huage.courseplatform.application.model.vo.CoursePeriodVO;
import com.huage.courseplatform.application.service.CourseService;
import com.huage.courseplatform.framework.core.impl.BaseServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CourseServiceImpl extends BaseServiceImpl<CourseMapper, Course> implements CourseService {
    @Resource
    CourseMapper courseMapper;

    @Resource
    BusinessMapper businessMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCourse(Course course,List<String> periodIdList) {
        save(course);
        String courseId = course.getId();
        periodIdList.forEach(periodId -> {
           courseMapper.addPeriodCourse(periodId,courseId);
        });
    }

    @Override
    @Cacheable(cacheNames="Service:CourseService:id",key = "#id",unless="#result == null")
    public CoursePeriodVO getCourseAndPeriodById(String id) {
        return courseMapper.getCourseAndPeriodById(id);
    }



    @Override
    @CacheEvict(cacheNames = "Service:CourseService:id", key = "#course.id")
    @Transactional(rollbackFor = Exception.class)
    public void updateCourse(Course course,List<String> periodIdList) {
        //todo 发售后不允许修改
        courseMapper.updateById(course);
        if(!CollectionUtils.isEmpty(periodIdList)){
            courseMapper.delPeriodCourseByCourseId(course.getId());
            periodIdList.forEach(periodId -> {
                courseMapper.addPeriodCourse(periodId,course.getId());
            });
        }

    }

    //无缓存
    @Override
    public Page<CourseDTO> coursePage(Page page, QueryWrapper queryWrapper) {
       return courseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public List<String> getUserIdListByCourseId(String courseId) {
        Course course = courseMapper.selectById(courseId);
        Assert.isTrue(ObjectUtils.isNotEmpty(course),"找不到相应课程");
        String businessId = course.getBusinessId();
        businessMapper.getUserIdListById(businessId);
        return businessMapper.getUserIdListById(businessId);
    }



}
